#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 28 déc. 2011

@author: nathanael
'''
import unittest

import re

from pybible.core.regex import CustomWordPattern, RegexPattern, WordPattern


def match_unicode(a, b):
    return re.match(a, b, re.UNICODE)

class TestRegex(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_regex(self):
        "test identity"
        builder = RegexPattern()
        self.assertEqual(builder.build_regex(u"regex"), u"regex")
        self.assertEqual(builder.build_regex("regex"), u"regex")

    def test_word(self):
        "match word only"
        pattern = WordPattern().build_regex(u"regex")
        self.assert_(match_unicode(pattern, u"") is None)
        self.assert_(match_unicode(pattern, u"regex") is not None)
        self.assert_(match_unicode(pattern, u" regex") is not None)
        self.assert_(match_unicode(pattern, u"regex ") is not None)
        self.assert_(match_unicode(pattern, u"a valide regex.") is not None)

    def test_custom(self):
        "custom regex builder"
        pattern_builder = CustomWordPattern(whole_word=False,
                                            case_sensitive=True,
                                            emphasis_characters=[])
        pattern = pattern_builder.build_regex(u"regex")

        self.assert_(match_unicode(pattern, u"") is None)
        self.assert_(match_unicode(pattern, u"regex") is not None)
        self.assert_(match_unicode(pattern, u" regex") is not None)
        self.assert_(match_unicode(pattern, u"regex ") is not None)
        self.assert_(match_unicode(pattern, u"a valide regex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide Regex.") is None)
        self.assert_(match_unicode(pattern, u"a valide régex.") is None)
        self.assert_(match_unicode(pattern, u"a valide aregex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide regexa") is not None)


        pattern_builder = CustomWordPattern(whole_word=True,
                                            case_sensitive=True,
                                            emphasis_characters=[])
        pattern = pattern_builder.build_regex(u"regex")

        self.assert_(match_unicode(pattern, u"") is None)
        self.assert_(match_unicode(pattern, u"regex") is not None)
        self.assert_(match_unicode(pattern, u" regex") is not None)
        self.assert_(match_unicode(pattern, u"regex ") is not None)
        self.assert_(match_unicode(pattern, u"a valide regex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide Regex.") is None)
        self.assert_(match_unicode(pattern, u"a valide régex.") is None)
        self.assert_(match_unicode(pattern, u"a valide aregex.") is None)
        self.assert_(match_unicode(pattern, u"a valide regexa") is None)


        pattern_builder = CustomWordPattern(whole_word=False,
                                            case_sensitive=False,
                                            emphasis_characters=[])
        pattern = pattern_builder.build_regex(u"regex")

        self.assert_(match_unicode(pattern, u"") is None)
        self.assert_(match_unicode(pattern, u"regex") is not None)
        self.assert_(match_unicode(pattern, u" regex") is not None)
        self.assert_(match_unicode(pattern, u"regex ") is not None)
        self.assert_(match_unicode(pattern, u"a valide regex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide Regex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide régex.") is None)
        self.assert_(match_unicode(pattern, u"a valide aregex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide regexa") is not None)


        pattern_builder = CustomWordPattern(whole_word=True,
                                            case_sensitive=False,
                                            emphasis_characters=[])
        pattern = pattern_builder.build_regex(u"regex")

        self.assert_(match_unicode(pattern, u"") is None)
        self.assert_(match_unicode(pattern, u"regex") is not None)
        self.assert_(match_unicode(pattern, u" regex") is not None)
        self.assert_(match_unicode(pattern, u"regex ") is not None)
        self.assert_(match_unicode(pattern, u"a valide regex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide Regex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide régex.") is None)
        self.assert_(match_unicode(pattern, u"a valide aregex.") is None)
        self.assert_(match_unicode(pattern, u"a valide regexa") is None)


        pattern_builder = CustomWordPattern(whole_word=False,
                                            case_sensitive=True,
                                            emphasis_characters=[u"eéèêë"])
        pattern = pattern_builder.build_regex(u"regex")

        self.assert_(match_unicode(pattern, u"") is None)
        self.assert_(match_unicode(pattern, u"regex") is not None)
        self.assert_(match_unicode(pattern, u" regex") is not None)
        self.assert_(match_unicode(pattern, u"regex ") is not None)
        self.assert_(match_unicode(pattern, u"a valide regex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide Regex.") is None)
        self.assert_(match_unicode(pattern, u"a valide régex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide aregex.") is not None)
        self.assert_(match_unicode(pattern, u"a valide regexa") is not None)



if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
